package com.swallow.auth.infrastructure.mysql.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swallow.auth.infrastructure.mysql.mapper.ThirdAccountMapper;
import com.swallow.auth.infrastructure.mysql.po.BasicOAuth2User;
import com.swallow.auth.infrastructure.mysql.po.ThirdAccountPO;
import com.swallow.auth.infrastructure.mysql.service.IAccountService;
import com.swallow.auth.infrastructure.mysql.service.IThirdAccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author: yangjie.deng@resico.cn
 * @since: 2024-05-13 16:26:25
 * @version: v1.0.0
 * @describe:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ThirdAccountServiceImpl extends ServiceImpl<ThirdAccountMapper, ThirdAccountPO> implements IThirdAccountService {

    private final IAccountService basicUserService;

    @Override
    public void checkAndSaveUser(BasicOAuth2User oAuth2User) {

        log.info("操作账户信息：： {}", oAuth2User);
        /*
        // 构建三方唯一id和三方登录方式的查询条件
        ThirdAccountPO thirdAccount = this.lambdaQuery().eq(ThirdAccountPO::getType, oAuth2User.getType())
                                                          .eq(ThirdAccountPO::getUniqueId, oAuth2User.getUniqueId())
                                                          .one();

        if (thirdAccount == null) {
            // 生成用户信息
            Integer userId = basicUserService.saveByThirdAccount(thirdAccount);
            thirdAccount.setUserId(userId);
            // 不存在保存用户信息
            this.save(thirdAccount);
        } else {
            // 校验是否需要生成基础用户信息
            if (ObjectUtils.isEmpty(thirdAccount.getAccountNo())) {
                // 生成用户信息
                Integer userId = basicUserService.saveByThirdAccount(thirdAccount);
                oauth2ThirdAccount.setUserId(userId);
            }
            // 存在更新用户的认证信息
            oauth2ThirdAccount.setCredentials(thirdAccount.getCredentials());
            oauth2ThirdAccount.setCredentialsExpiresAt(thirdAccount.getCredentialsExpiresAt());
            // 设置空， 让MybatisPlus自动填充
            oauth2ThirdAccount.setUpdateTime(null);
            this.updateById(oauth2ThirdAccount);
        }

        oAuth2User.setName(oAuth2User.getName());
        oAuth2User.setSourceFrom(oAuth2User.getType());*/
    }

}
